--[[ FireFlowerCoins.lua v1.1
Created by: EeveeEuphoria

Usage:
    Place this line into your global lua level file:

    local ffc = require("Libraries.FireFlowerCoins")
    
    When used, any enemy that isn't a boss will drop coins when harmed.

    - ffc.coinID is the ID used to spawn the coin NPC in question; it's set to 10 by default, for SMB3's coin.
    - ffc.changeai1 is set to true; if set to false, it will not modify the ai1 parameter of the NPC, which is useful if you're not using a coin.
    - ffc.fireballID is the ID used to track what NPC is the player fireball; if an enemy was killed by other means, it will not spawn a coin
    - ffc.speedX1 and ffc.speedX2 is used as RNG values to determine how fast the coins fly away from the enemy.
        - Set both of these to 0 if you want them to just fly up and then back down.
    - ffc.npccointable is a table of NPCs that changes the ordinairy coin that would spawn on their death
        - If the value for a key is set to 0, then nothing will spawn for that NPC.
        - Example:
        ffc.npccointable = { [175] = 88,
            [19] = 138,
            [466] = 0,
        }
    - ffc.disablenpccointable will disable the usage of the npccointable, thus meaning every enemy drops the same coin as provided by ffc.coinID.
        - This will not disable ffc.npccointable however.

]]

local ffc = {}

ffc.coinID = 10
ffc.changeai1 = true
ffc.fireballID = 13
ffc.speedX1 = 0.65
ffc.speedX2 = 1

local npccointable = { -- used to drop different coin NPCs dependant on the type of enemy hurt by fireballs
    -- SMB1
    [175] = 88,
    [173] = 88,
    [89] = 88,
    [176] = 88,
    [177] = 88,
    [27] = 88,
    [29] = 88,
    [174] = 88,
    [172] = 88,
    [93] = 88,
    [28] = 88,
    [233] = 88,
    [610] = 88,
    [611] = 88,
    [612] = 88,
    [235] = 88,
    -- SMB2, just for in case they're set to killable by fireball
    [19] = 138,
    [20] = 138,
    [130] = 138,
    [131] = 138,
    [132] = 138,
    [470] = 138,
    [471] = 138,
    [25] = 138,
    [272] = 138,
    [530] = 138,
    [135] = 138,
    [129] = 138,
    [350] = 138,
    [374] = 138,
    -- SMW
    [165] = 33,
    [166] = 33,
    [167] = 33,
    [162] = 33,
    [163] = 33,
    [284] = 33,
    [285] = 33,
    [286] = 33,
    [380] = 33,
    [431] = 33,
    [408] = 33,
    [409] = 33,
    [382] = 33,
    [383] = 33,
    [270] = 33,
    [275] = 33,
    [407] = 33,
    [77] = 33,
    [18] = 33,
    [309] = 33,
    [360] = 33,
    [446] = 33,
    [448] = 33,
    [333] = 33,
    [358] = 33,
    [117] = 33,
    [118] = 33,
    [119] = 33,
    [120] = 33,
    [113] = 33,
    [114] = 33,
    [115] = 33,
    [116] = 33,
    [194] = 33,
    [195] = 33,
    [109] = 33,
    [110] = 33,
    [111] = 33,
    [112] = 33,
    [121] = 33,
    [122] = 33,
    [123] = 33,
    [124] = 33,
    [302] = 33,
    [236] = 33,
    [386] = 33,
    [459] = 33,
    [460] = 33,
    [461] = 33,
}

ffc.npccointable = {}

function ffc.onInitAPI()
    registerEvent(ffc, "onTick")
	registerEvent(ffc, "onPostNPCHarm")
end

function ffc.onPostNPCHarm(NPC, harmType, culpritOrNil) 
    if not culpritOrNil then return end
    if harmType ~= HARM_TYPE_NPC then return end
    if NPC.id == ffc.fireballID then return end
    if culpritOrNil.id ~= ffc.fireballID then return end
    if NPC:mem(0x148, FIELD_FLOAT) > 0 then return end

    local coinvalue = ffc.coinID
    if not ffc.disablenpccointable then
        if npccointable[NPC.id] then
            coinvalue = npccointable[NPC.id]
        end
    end
    if ffc.npccointable[NPC.id] then
        coinvalue = ffc.npccointable[NPC.id]
    end
    
    if coinvalue == 0 then return end
    local coinspawn = NPC.spawn(coinvalue, NPC.x, NPC.y, NPC.section)
    coinspawn.speedX = RNG.random(ffc.speedX1, ffc.speedX2) * RNG.random(-1, 1)
    coinspawn.speedY = -5 + RNG.random(-0.8, -0.2)
    if ffc.changeai1 then coinspawn.ai1 = 1 end
end


return ffc